﻿using System;

namespace LiveNet.Silverlight
{
    /// <summary>
    /// 
    /// </summary>
    public class MediaFileSet
    {
        private readonly string m_Name;
        private readonly bool m_IsApplication;
        private readonly string m_AccountId;
        internal readonly LiveMedia m_LiveMedia;
        private bool m_Deleted;

        internal MediaFileSet(LiveMedia liveMedia, string accountId, string name, bool isApplication)
        {
            m_LiveMedia = liveMedia;
            m_Name = name;
            m_IsApplication = isApplication;
            m_AccountId = accountId;
        }

        #region Public Properties
        /// <summary>
        /// Gets the account id.
        /// </summary>
        /// <value>The account id.</value>
        public string AccountId
        {
            get { return m_AccountId; }
        }
        
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return m_Name; }
        }

        /// <summary>
        /// Gets the iFrame code.
        /// </summary>
        /// <value>The iFrame code.</value>
        /// <remarks>
        /// This will be in the HTML format:
        /// <iframe src=\"http://silverlight.services.live.com/invoke/AccountID/AppName/iframe.html" frameborder="0" />
        /// </remarks>
        public string IFrameCode
        {
            get
            {
                return
                    string.Format(
                        "<iframe src=\"http://silverlight.services.live.com/invoke/{0}/{1}/iframe.html\" frameborder=\"0\" />",
                        AccountId, Name);
            }
        }

        /// <summary>
        /// Gets the iFrame URL.
        /// </summary>
        /// <value>The iFrame URL.</value>
        /// <remarks>
        /// This will be in the format http://silverlight.services.live.com/invoke/AccountID/AppName/iframe.html
        /// </remarks>
        public Uri IFrameUrl
        {
            get
            {
                return
                    new Uri(
                        string.Format("http://silverlight.services.live.com/invoke/{0}/{1}/iframe.html", AccountId, Name));
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is application.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is application; otherwise, <c>false</c>.
        /// </value>
        public bool IsApplication
        {
            get { return m_IsApplication; }
        }

        /// <summary>
        /// Gets the file set URL.
        /// </summary>
        /// <value>The file set URL.</value>
        public Uri FileSetUrl
        {
            get { return new Uri(string.Format("https://silverlight.services.live.com/{0}/{1}", AccountId, Name)); }
        }

        /// <summary>
        /// Gets the files of the Set.
        /// </summary>
        /// <value>The files.</value>
        /// <example>
        /// <code>
        /// // Create your LiveMedia instance
        /// LiveMedia m_liveMedia = new LiveMedia("AccountID", "AccountKey");
        /// 
        /// string appname = "My New App";
        /// MediaFileSet set = m_liveMedia.GetFileSet(appname);
        /// 
        /// // Loop through the files for this set and display the filename
        /// foreach(MediaFile file in set.Files)
        /// {
        ///     Console.WriteLine(file.FileName);
        /// }
        /// </code>
        /// </example>
        public MediaFile[] Files
        {
            get
            {
                if (HasBeenDeleted)
                {
                    throw new InvalidOperationException(string.Format(LiveNetResources.AlbumDeletedListException, "photos"));
                }

                return m_LiveMedia.GetFiles(this);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has been deleted.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has been deleted; otherwise, <c>false</c>.
        /// </value>
        public bool HasBeenDeleted
        {
            get { return m_Deleted; }
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// Deletes this instance.
        /// </summary>
        /// <example>
        /// <code>
        /// // Create your LiveMedia instance
        /// LiveMedia m_liveMedia = new LiveMedia("AccountID", "AccountKey");
        /// 
        /// string appname = "My New App";
        /// 
        /// MediaFileSet set = m_liveMedia.GetFileSet(appname);
        /// set.Delete();
        /// </code>
        /// </example>
        public void Delete()
        {
            if (!HasBeenDeleted && m_LiveMedia.DeleteFileSet(FileSetUrl))
            {
                m_Deleted = true;
            }
        }
        #endregion
    }
}
